package cn.kduck.plugin.idea.utils;

public final class StringUtils {

    private StringUtils() {}

    public static String upperFirstChar(String str) {
        char[] charArray = str.toCharArray();
        charArray[0] = Character.toUpperCase(charArray[0]);
        return new String(charArray);
    }

    public static String lowerFirstChar(String str) {
        char[] charArray = str.toCharArray();
        charArray[0] = Character.toLowerCase(charArray[0]);
        return new String(charArray);
    }

    public static String getWordFirstChar(String str) {
        return getWordFirstChar(str,'_');
    }

    public static String getWordFirstChar(String str,char separator) {
        String[] split = str.split("[" + separator + "]");
        StringBuilder sb = new StringBuilder();
        for (String word : split) {
            sb.append(word.charAt(0));
        }
        return sb.toString().toLowerCase();
    }

    public static String clearSeparator(String str,char separator) {
        String[] split = str.split("[" + separator + "]");
        StringBuilder sb = new StringBuilder(split[0].toLowerCase());
        for (int i = 1; i < split.length; i++) {
            sb.append(upperFirstChar(split[i].toLowerCase()));
        }
        return sb.toString();
    }

    public static String lowerCaseWithSeparator(String str,String separator){
        if(str == null){
            return "";
        }
        separator = separator == null?"":separator;

        char[] charArray = str.toCharArray();
        String result = "";
        boolean upper = false;
        for (int i = 0; i < charArray.length; i++) {
            if(i != 0 && charArray[i] == separator.charAt(0)){
                upper = true;
                continue;
            }
            if(upper){
                result += Character.toUpperCase(charArray[i]);
                upper = false;
            }else{
                result += Character.toLowerCase(charArray[i]);
            }
        }
        return result;
    }

    public static String upperCaseWithSeparator(String str,String separator){
        if(str == null){
            return "";
        }
        separator = separator == null?"":separator;

        char[] charArray = str.toCharArray();
        String result = "";
        for (int i = 0; i < charArray.length; i++) {
            if(i != 0 &&
                    charArray[i] == Character.toUpperCase(charArray[i]) &&
                    charArray[i-1] != Character.toUpperCase(charArray[i-1])){
                result += separator;
            }
            result += Character.toUpperCase(charArray[i]);
        }
        return result;
    }
}